import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login'),
    },
    {
      path: '/',
      component: () => import('@/views/layout'),
      redirect: 'homepage',
      children: [
        {
          path: 'homepage',
          component: () => import('@/views/second/homePage'),
        },
        {
          path: '/region',
          component: () => import('@/views/second/regionPage')
        },
        {
          path: '/point',
          component: () => import('@/views/second/pointPage')
        },
        {
          path: '/partners',
          component: () => import('@/views/second/partners')
        }
        
      ],
    },
  ],
})

// 全局路由守卫
const whiteRoute = ['/login']
router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  if (token) {
    next()
  } else {
    if (whiteRoute.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
